package com.ccqlk.basemainapp.mas;

import org.apache.poi.xwpf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.List;

public class 云mas工时单铁通转鲁控 {
    private static final Logger log = LoggerFactory.getLogger(云mas工时单铁通转鲁控.class);
    private static final List<String> SPECIAL_LIST = Arrays.asList("陆军军医大学第一附属医院","陆军军医大学第二附属医院","重庆市公安局沙坪坝分局","重庆市沙坪坝区人民政府办公室","重庆智算科技");


    public static void main(String[] args) throws Exception {
        File file = new File("E:\\cqj\\工时相关\\2024\\2024云mas未签工时单-鲁控 至 铁通");
        File[] files = file.listFiles();

        execute(files);

    }



    public static void execute(File[] files) throws Exception {
        if(files == null || files.length < 1){
            return;
        }

        for (File file : files) {
            try {
                if(file.isDirectory()){
                    File[] childFiles = file.listFiles();
                    execute(childFiles);
                } else {
                    updateFile(file);
                }
            } catch (Exception e) {
                log.error("file deal error,file name:{}",file,e);
            }
        }
    }

    public static void updateFile(File file) throws Exception {
        String fileName = file.getName();
        boolean isFile = file.isFile();
        // 处理工时表
        if(isFile && fileName.contains("docx")){
            try (FileInputStream fis = new FileInputStream(file);
                 XWPFDocument document = new XWPFDocument(fis)) {

                if(fileName.contains("2024-")){
                    List<XWPFTable> tables = document.getTables();
                    // 西南医院第一个表格更改为城三
                    XWPFTable table1 = tables.getFirst();
                    XWPFTableRow table1Row2 = table1.getRow(1);
                    String enterpriseName = table1Row2.getCell(1).getText();
                    boolean anyMatch = SPECIAL_LIST.stream().anyMatch(enterpriseName::contains);
                    if(anyMatch){
                        XWPFTableCell xwpfTableCell = table1.getRow(0).getCell(1);
                        String companyName = xwpfTableCell.getText();
                        if(!companyName.contains("沙坪坝分公司")){
                            setCellValue(xwpfTableCell,"中国移动通信集团重庆有限公司城三分公司",false);
                            log.info("file name: {},enterpriseName :{},reset company name ok",fileName,enterpriseName);
                        }
                    }

                    // 获取最后一个表格更改为铁通签字
                    XWPFTable table4 = tables.get(3);
                    int size = table4.getRows().size();
                    if(size == 2){
                        log.info("file name: {},enterpriseName :{},has been deal,continue,",fileName,enterpriseName);
                        return;
                    }

                    // 删除第一行
                    table4.removeRow(0);
                    XWPFTableRow tableRow = table4.getRow(0);
                    XWPFTableCell cell = tableRow.getCell(0);
                    setCellValue(cell,"铁通签字",true);
                    log.info("file name: {},enterpriseName：{}，set to 铁通签字 ok",fileName,enterpriseName);
                } else if(fileName.contains("云MAS客户需求工时量表")){


                    for (int i = document.getParagraphs().size() - 1; i >= 0; i--) {
                        XWPFParagraph para = document.getParagraphs().get(i);
                        for (XWPFRun run : para.getRuns()) {
                            String text = run.getText(0);
                            if (text != null && text.contains("移动")) {
                                String replaced = text.replace("移动", "铁通");
                                run.setText(replaced, 0);
                                String enterpriseName = document.getTables().getFirst().getRow(0).getCell(1).getText();
                                log.info("file name: {},enterpriseName：{}，set to 铁通 ok",fileName,enterpriseName);
                                break;
                            }
                        }
                    }
                }


                boolean delete = file.delete();
                if(delete){
                    if(fileName.contains("云MAS客户满意度报告")){
                        return;
                    }

                    try (FileOutputStream fos = new FileOutputStream(file.getParent() + File.separator + fileName)) {
                        document.write(fos);
                    }
                }
            }
        }
    }

    public static void setCellValue(XWPFTableCell cell, String value,boolean isCenter) {
        cell.removeParagraph(0);
        XWPFParagraph paragraph = cell.addParagraph();
        if(isCenter){
            paragraph.setAlignment(ParagraphAlignment.CENTER);
        }
        XWPFRun run = paragraph.createRun();

        // 设置内容和样式
        run.setText(value);
        run.setFontFamily("宋体");         // 设置字体为宋体
        run.setFontSize(9);               // 小五号字体（9pt）
        run.setBold(true);
    }
}
